/*
* $Id: UserFieldsManager.java 278 2011-03-04 22:31:44Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2011 by Volokitin Alexey
* All rights reserved
*
*/
package apiary.db.managers;

import apiary.db.UserFieldsStore;
import apiary.db.UserFieldsStore.UserFieldsRecord;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

/**
 * User's fields manager
 * @author lexus
 */
public class UserFieldsManager
{
    /** instance class */
    private static UserFieldsManager instance = null;
    /**
     * Gets instance class
     * @return instance class
     */
    protected static UserFieldsManager getInstance() {
        return instance;
    }

    /** user's fields database */
    private UserFieldsStore userFieldsStore;
    /** list of user's fields */
    private Hashtable dbUserField;

    /**
     * Constructor
     */
    public UserFieldsManager() {
        instance = this;
        userFieldsStore = new UserFieldsStore();
        dbUserField = new Hashtable();
        readUserFieldStore();
    }

    /**
     * Gets user's fields for section
     * @param table field section
     * @return list of user's fields
     */
    public Vector getUserFields(int table) {
        Integer tbl = new Integer(table);
        if(dbUserField.containsKey(tbl))
            return (Vector) dbUserField.get(tbl);
        return new Vector();
    }

    /**
     * Resets all data in manager
     */
    protected void resetManager() {
        dbUserField = new Hashtable();
        readUserFieldStore();
    }

    /**
     * Reads user's fields database
     */
    private void readUserFieldStore() {
        Enumeration userFieldsEnum = userFieldsStore.getRecordsEnumeration();
        do {
            if(!userFieldsEnum.hasMoreElements())
                break;
            UserFieldsRecord record = (UserFieldsRecord) userFieldsEnum.nextElement();
            addHashtableRecord(record.table, record);
        } while(true);
    }

    /**
     * Adds user's field at list
     * @param idRec section of field
     * @param record user's field
     */
    private void addHashtableRecord(int idRec, UserFieldsRecord record) {
        Integer idRecord = new Integer(idRec);
        if(!dbUserField.containsKey(idRecord))
            dbUserField.put(idRecord, new Vector());
        Vector fieldsValue = (Vector) dbUserField.get(idRecord);
        fieldsValue.addElement(record);
    }
}
